package com.shanshi.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.shanshi.activity.adapter.CommentAdapter;
import com.shanshi.activity.adapter.HeadPicAdapter;
import com.shanshi.activity.request.EssayInfo;
import com.shanshi.activity.tool.CheckView;
import com.shanshi.activity.tool.CommentDialog;
import com.shanshi.activity.tool.OkHttpTool;

import java.util.ArrayList;
import java.util.List;

import static com.shanshi.activity.R.drawable.praise;




public class EssayInfoActivity extends Activity {

    private String articleId;
    private SharedPreferences sharedPreferences;
    private String userId;
    private List<EssayInfo.DataBean> list = new ArrayList<>();
    private List<EssayInfo.DataBean.ArticlePraiseUserBean> listhead = new ArrayList<>();
    private TextView title, tv_allcomment, tv_writecomment;
    private ImageView fristpic, img_praise;
    private EssayInfo.DataBean dataBean;
    private RecyclerView rec_parsie, rec_comment;
    private List<EssayInfo.DataBean.ContentArrayBean> listcontentArrayBeen = new ArrayList<>();
    private List<EssayInfo.DataBean.CommentArrayBean> listcommentArrayBeen = new ArrayList<>();
    private EssayInfo.DataBean.ContentArrayBean contentArrayBeen;
    private LinearLayout linearLayout;
    private CommentAdapter commentAdapter;
    private EditText message;
    private Gson gson;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                if (dataBean != null) {
                    title.setText(dataBean.getTitle());
                    Glide.with(EssayInfoActivity.this).load(dataBean.getImgUrl()).into(fristpic);
                    listcontentArrayBeen = dataBean.getContentArray();
                    listcommentArrayBeen = dataBean.getCommentArray();
                    for (int i = 0; i < listcontentArrayBeen.size(); i++) {
                        contentArrayBeen = listcontentArrayBeen.get(i);
                        if (contentArrayBeen.getType() == 1) {
                            TextView textView = new TextView(EssayInfoActivity.this);
                            textView.setText(contentArrayBeen.getContent());
                            linearLayout.addView(textView);
                        } else if (contentArrayBeen.getType() == 2) {
                            ImageView imageView = new ImageView(EssayInfoActivity.this);
                            Glide.with(EssayInfoActivity.this).load("http://183.222.214.218:44441" + contentArrayBeen.getContent()).into(imageView);
                            Log.e("pic====", "handleMessage: " + "http://183.222.214.218:44441" + contentArrayBeen.getContent());
                            linearLayout.addView(imageView);
                        } else if (contentArrayBeen.getType() == 3) {
                            TextView textView = new TextView(EssayInfoActivity.this);
                            textView.setText("视频");
                            textView.setTextSize(30);
                            linearLayout.addView(textView);
                        } else if (contentArrayBeen.getType() == 4) {
                            List<EssayInfo.DataBean.ContentArrayBean.Answer> list = new ArrayList<>();
                            list = contentArrayBeen.getAnswer();
                            CheckView checkView = new CheckView(EssayInfoActivity.this);
                            checkView.initData(list, EssayInfoActivity.this, contentArrayBeen.getContent(),userId,contentArrayBeen.getId()+"");
                            linearLayout.addView(checkView);
                        }
                    }
                    if (dataBean.isPraiseState()) {
                        img_praise.setImageResource(R.drawable.praise_no);
                    } else {
                        img_praise.setImageResource(praise);
                    }
                    tv_allcomment.setText("全部回复" + "  " + dataBean.getReplyCount());
                    tv_parise.setText(dataBean.getPraiseCount()+"");
                    listcommentArrayBeen = dataBean.getCommentArray();
                    commentAdapter = new CommentAdapter(listcommentArrayBeen, EssayInfoActivity.this, userId, articleId);
                    rec_comment.setAdapter(commentAdapter);
                    listhead = dataBean.getArticlePraiseUser();
                    headPicAdapter = new HeadPicAdapter(EssayInfoActivity.this, listhead);
                    rec_parsie.setAdapter(headPicAdapter);
                    commentAdapter.notifyDataSetChanged();
                    headPicAdapter.notifyDataSetChanged();
                }
            } else if (msg.what == 2) {
                //download(2);
                commentAdapter.notifyDataSetChanged();
                headPicAdapter.notifyDataSetChanged();
            }
        }
    };
    private HeadPicAdapter headPicAdapter;
    private TextView tv_parise;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_essay_info);
        Intent intent = getIntent();
        articleId = intent.getStringExtra("articleId");
        sharedPreferences = this.getSharedPreferences("share", MODE_PRIVATE);
        // TODO: 2017/3/21 userid
//        userId = sharedPreferences.getString("userId","");
        userId = "5";
        linearLayout = (LinearLayout) findViewById(R.id.linearLayout);
        title = (TextView) findViewById(R.id.tv_tssay_itle);
        fristpic = (ImageView) findViewById(R.id.image_essay_fristpic);
        tv_parise = (TextView) findViewById(R.id.tv_parise);
        Log.e("===id", "onCreate: " + articleId + "aaaa" + userId);
        rec_parsie = (RecyclerView) findViewById(R.id.rec_praise);
        rec_comment = (RecyclerView) findViewById(R.id.rec_comment);
        rec_parsie.setLayoutManager(new GridLayoutManager(this, 6));
        img_praise = (ImageView) findViewById(R.id.img_praise);

        img_praise.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (dataBean.isPraiseState()) {
                    img_praise.setImageResource(praise);
                    String rebody = gson.toJson("false");
                    OkHttpTool.doDelete(OkHttpTool.BASE_URL + "users/" + userId + "/articles/{articleId}/praise", rebody, new OkHttpTool.setOkhttpListener() {
                        @Override
                        public void onSuccess(String s) {
                            handler.sendEmptyMessage(2);
                        }

                        @Override
                        public void onFail() {

                        }
                    });
                    tv_parise.setText((dataBean.getPraiseCount() - 1)+"");
                } else {
                    img_praise.setImageResource(R.drawable.praise_no);
                    String rebody = gson.toJson("true");
                    OkHttpTool.doPost(OkHttpTool.BASE_URL + "users/" + userId + "/articles/" + articleId + "/praise", rebody, new OkHttpTool.setOkhttpListener() {
                        @Override
                        public void onSuccess(String s) {
                            handler.sendEmptyMessage(2);
                        }

                        @Override
                        public void onFail() {

                        }
                    });
                    tv_parise.setText((dataBean.getPraiseCount()) + 1 + "");
                }
            }
        });
        tv_allcomment = (TextView) findViewById(R.id.tv_allreply);
        tv_writecomment = (TextView) findViewById(R.id.tv_writecomment);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        };
        rec_comment.setLayoutManager(linearLayoutManager);
        download(1);
        tv_writecomment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialog();
            }
        });
    }

    public void download(final int i) {
        // TODO: 2017/3/21 添加userId
        OkHttpTool.doGet(OkHttpTool.BASE_URL + "articles/" + articleId + "/detail?userId=5", new OkHttpTool.setOkhttpListener() {


            @Override
            public void onSuccess(String s) {
                handler.sendEmptyMessage(i);
                EssayInfo essayInfo = new EssayInfo();
                gson = new Gson();
                essayInfo = gson.fromJson(s, EssayInfo.class);
                dataBean = new EssayInfo.DataBean();
                dataBean = essayInfo.getData();
            }

            @Override
            public void onFail() {

            }
        });
    }

    public void showDialog() {
        final CommentDialog commentDialog = new CommentDialog(
                this
        );
        message = commentDialog.getMessageEditText();
        commentDialog.setDialogclose(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                commentDialog.dismiss();
            }
        });
        commentDialog.setOnSendMessage(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (message.getText() != null) {
                    Log.e("======", "onFail: " + "评论成功");
                    String reply = message.getText().toString();
                    String rebody = gson.toJson(reply);
                    OkHttpTool.doPost(OkHttpTool.BASE_URL + "users/" + userId + "/articles/" + articleId + "/comment", rebody, new OkHttpTool.setOkhttpListener() {
                        @Override
                        public void onSuccess(String s) {
                            Log.e("======", "onSuccess: " + "评论成功");
                            handler.sendEmptyMessage(2);
                            if (commentDialog != null) {
                                commentDialog.dismiss();
                            }
                            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                            imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);

                        }

                        @Override
                        public void onFail() {
                            Log.e("======", "onFail: " + "评论失败");
                        }
                    });
                } else {
                    Toast.makeText(EssayInfoActivity.this, "评论不能为空~", Toast.LENGTH_SHORT).show();
                }
            }
        });
        commentDialog.show();
    }

}

